/**
 * 
 */
package com.jie.eclipse.pan.gef.dialog;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.custom.ViewForm;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Scrollable;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.ui.forms.events.ExpansionAdapter;
import org.eclipse.ui.forms.events.ExpansionEvent;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.wst.common.ui.internal.dnd.ObjectTransfer;
import org.eclipse.wst.common.ui.internal.dnd.ViewerDragAdapter;

import com.jie.eclipse.pan.bind.BaseDataBind;
import com.jie.eclipse.pan.dao.QMDao;
import com.jie.eclipse.pan.data.BaseDataEntity;
import com.jie.eclipse.pan.form.item.Tab;
import com.jie.eclipse.pan.form.support.FormItem;
import com.jie.eclipse.pan.gef.action.SeachActionGroup;
import com.jie.eclipse.pan.gef.action.SelectedColumnsActionGroup;
import com.jie.eclipse.pan.gef.listener.PanTableOrderDropAdapter;
import com.jie.eclipse.pan.listener.PFocusListener;
import com.jie.eclipse.pan.listener.PModifyListener;
import com.jie.eclipse.pan.listener.PMouseListener;
import com.jie.eclipse.pan.swtitem.table.PTable;
import com.jie.eclipse.pan.swtitem.table.PTableColumn;
import com.jie.eclipse.pan.swtitem.table.PTableViewer;
import com.jie.eclipse.pan.swtitem.table.PllTableContentProvider;
import com.jie.eclipse.pan.swtitem.table.PllTableLabelProvider;
import com.jie.eclipse.pan.swtitem.table.cellmodifier.SelectColumnsTableCellModifier;
import com.jie.eclipse.pan.swtitem.widgets.PanStringComboBoxCellEditor;
import com.jie.eclipse.pan.utils.ObjectHelper;
import com.jie.eclipse.pan.utils.StringBuilderEx;
import com.jie.eclipse.pan.utils.StringHelper;
import com.jie.eclipse.pan.utils.Utils;

/**
 * @author yanjie
 * 
 */
public class BasePanelDialog extends BaseDialog {
	private int curTabSize = 0;
	private TabFolder tabFolder = null;
	private Map<String, String> classDialogMap = null;
	private Map<String, String> classFormItemMap = null;

	/**
	 * @param parentShell
	 * @param data
	 */
	public BasePanelDialog(Shell parentShell, BaseDataEntity data,String action) {
		super(parentShell, data,action);
		this.classDialogMap = QMDao.getInstance().getXtypeDialogClassMap();
		this.classFormItemMap = QMDao.getInstance().getXtypeFormItemClassMap();
//		this.classMap.put("text", "com.jie.eclipse.pan.gef.dialog.TextDialog");
//		this.classMap.put("select", "com.jie.eclipse.pan.gef.dialog.SelectDialog");
//		this.classMap.put("checkbox", "com.jie.eclipse.pan.gef.dialog.CheckboxDialog");
//		this.classMap.put("radio", "com.jie.eclipse.pan.gef.dialog.RadioDialog");
	}

	@Override
	public void initDefaultData() {
		super.initDefaultData();
		BaseDataEntity defaultData = this.getDefaultData();
		if (defaultData == null)
			defaultData = new BaseDataEntity();
		defaultData.spsv("tabSize", "1");
		this.setDefaultData(defaultData);
	}

	@Override
	protected Point getInitialSize() {
		// Point point = super.getInitialSize();
		// point.x = 600;
		return new Point(600, 600);
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		Color bgcolor = Display.getDefault().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
		ScrolledComposite  form = new ScrolledComposite  (parent, SWT.BORDER | SWT.V_SCROLL);
		form.setLayout(new GridLayout(1, false));
		form.setLayoutData(new GridData(GridData.FILL_BOTH));
		form.setAlwaysShowScrollBars(true);  
		form.setExpandHorizontal(true);
		form.setExpandVertical(true);
		form.setMinHeight(420);  
		
		Composite composite = new Composite (form, SWT.NONE);
		composite.setLayout(new GridLayout(6, false));
		composite.setLayoutData(new GridData(GridData.FILL_BOTH));
		composite.setBackground(bgcolor);
		Label lb = null;
		GridData gd = null;
		Group group = null;
		
		lb=  new Label(composite,SWT.RIGHT);
		lb.setText("ID:");
		lb.setBackground(bgcolor);
		Text id = new Text(composite, SWT.BORDER);
		id.setLayoutData(new GridData(60, 15));
		this.getDataBind().bind("id", id);	
		
		lb=  new Label(composite,SWT.RIGHT);
		lb.setText("页数(tabSize):");
		lb.setBackground(bgcolor);
		Text tabSize = new Text(composite, SWT.BORDER);
		tabSize.setLayoutData(new GridData(60, 15));
		tabSize.addModifyListener(new PModifyListener(null){
			@Override
			public void modifyText(ModifyEvent paramModifyEvent) {
				Text source = (Text)paramModifyEvent.getSource();
				if(source.isFocusControl()){
					source.setData("change", true);
				}
			}
		});
		BaseDataEntity data = new BaseDataEntity();
		data.spov("bind", this.getDataBind());
		data.spov("dialog", this);
		tabSize.addFocusListener(new PFocusListener(data){
			@Override
			public void focusLost(FocusEvent paramFocusEvent) {
				Object obj = paramFocusEvent.getSource();
				if(obj instanceof Scrollable){
					Text source = (Text)paramFocusEvent.getSource();
					if(source.getData("change") != null && (Boolean)source.getData("change")){
						source.setData("change", null);
						BaseDataBind bind = (BaseDataBind)this.data.gpov("bind");
						String value = source.getText();
						if(!bind.getRecord().isNullOrEmpty("customtab") 
								|| !StringHelper.isInt(value) 	
								|| Integer.parseInt(value) >2 
								|| Integer.parseInt(value) <0){
							source.setText(String.valueOf(getCurTabSize()));
							return ;
						}
						int tabsize = Integer.parseInt(value);
						TabFolder tabFolder = getTabFolder();
						if(tabsize == 0 && getCurTabSize() != 0){
							tabFolder.getItem(0).setText("空标签页");
							if(getCurTabSize() == 2){
								getDataBind().unbind((String)tabFolder.getItem(1).getData("id"));
								tabFolder.getItem(1).dispose();
							}
							tabFolder.setSelection(0);
							setCurTabSize(tabsize);
						}else if(tabsize == 1 && getCurTabSize() != 1){
							if(getCurTabSize() == 0){
								tabFolder.getItem(0).setText(getFirstTabName());
							}else if(getCurTabSize() == 2){
								getDataBind().unbind((String)tabFolder.getItem(1).getData("id"));
								tabFolder.getItem(1).dispose();
							}
							setCurTabSize(tabsize);
						}else if(tabsize == 2 && getCurTabSize() != 2){
							tabFolder.getItem(0).setText(getFirstTabName());
							
							if(getCurTabSize() == 1){
								BasePanelDialog dialog = (BasePanelDialog)this.data.gpov("dialog");
								createTab(dialog, tabFolder, "tab2", getSecTabName());
							}
							setCurTabSize(tabsize);
						}
						source.setText(String.valueOf(getCurTabSize()));
						return;
					}
				}
			}
		});
		
		this.getDataBind().bind("tabSize", tabSize);
		
		
//		lb=  new Label(composite,SWT.RIGHT);
//		lb.setText("列数(colModel):");
//		lb.setBackground(bgcolor);
//		Text colModel = new Text(composite, SWT.BORDER);
//		colModel.setLayoutData(new GridData(60, 15));
//		this.getDataBind().bind("colModel", tabSize);
		
		new Label(composite,SWT.RIGHT);
		new Label(composite,SWT.RIGHT);
		
		lb=  new Label(composite,SWT.RIGHT);
		lb.setText("自定义标签页(customtab):");
		lb.setBackground(bgcolor);
		lb.setLayoutData( new GridData(80, 30));
		Text customtab = new Text(composite, SWT.BORDER);
		gd = new GridData(360, 15);
		gd.horizontalSpan=5;
		customtab.setLayoutData(gd);
		customtab.addModifyListener(new PModifyListener(null){
			@Override
			public void modifyText(ModifyEvent paramModifyEvent) {
				Text source = (Text)paramModifyEvent.getSource();
				if(source.isFocusControl()){
					source.setData("change", true);
				}
			}
		});
		customtab.addFocusListener(new PFocusListener(data){
			@Override
			public void focusLost(FocusEvent paramFocusEvent) {
				Object obj = paramFocusEvent.getSource();
				if(obj instanceof Scrollable){
					Text source = (Text)paramFocusEvent.getSource();
					if(source.getData("change") != null && (Boolean)source.getData("change")){
						source.setData("change", null);
						BaseDataBind bind = (BaseDataBind)this.data.gpov("bind");
						String value = source.getText();
						TabFolder tabFolder = getTabFolder();
						if(StringHelper.IsNullOrEmpty(value)){
							tabFolder.getItem(0).setText(getFirstTabName());
							if(getCurTabSize() >= 1){
								for (int i = 1; i <= getCurTabSize(); i++) {
									getDataBind().unbind((String)tabFolder.getItem(i).getData("id"));
									tabFolder.getItem(i).dispose();
								}
							}
							tabFolder.setSelection(0);
							bind.getTextCtr("tabSize").setText("1");
							setCurTabSize(1);
						}else{
							String tabnams[] = value.split("[|]");
							int tabsize = tabnams.length;
							bind.getTextCtr("tabSize").setText(String.valueOf(tabsize));
							
//							Utils.print("value="+value);
//							Utils.print("tabsize="+tabsize);
//							Utils.print("getCurTabSize="+getCurTabSize());
							
							if(tabsize <= getCurTabSize()){
								for (int i = 0; i < getCurTabSize(); i++) {
									if(i<tabsize){
										tabFolder.getItem(i).setText(tabnams[i]);
									}else{
										getDataBind().unbind((String)tabFolder.getItem(i).getData("id"));
										tabFolder.getItem(i).dispose();
									}
								}
								tabFolder.setSelection(0);
								setCurTabSize(tabsize);
							}else if(tabsize > getCurTabSize()){
								for (int i = 0; i < tabsize; i++) {
									if(i<getCurTabSize()){
										tabFolder.getItem(i).setText(tabnams[i]);
									}else{
										BasePanelDialog dialog = (BasePanelDialog)this.data.gpov("dialog");
										createTab(dialog, tabFolder, "tab"+(i+1), tabnams[i]);
									}
								}
								tabFolder.setSelection(0);
								setCurTabSize(tabsize);
							}
						}
						return;
					}
				}
			}
		});
		
		this.getDataBind().bind("customtab", customtab);
		
		group = new Group(composite, SWT.NULL);
		group.setText("标签页属性");
		FillLayout layout = new FillLayout();
		group.setLayout(layout);
		
		gd = new GridData(GridData.FILL_BOTH);
		gd.horizontalSpan=5;
		group.setLayoutData(gd);
		
		tabFolder = new TabFolder(group, SWT.NONE);
		tabFolder.setBackground(bgcolor);
		createTab(this,tabFolder,"tab0",this.getFirstTabName());
		data.spov("tabFolder", tabFolder);
		this.setCurTabSize(1);
        this.reloadRecord();
        form.setContent(composite);
		return parent;
	}

	public void createTab(BaseDialog dialog, TabFolder tabFolder, String tabid,
			String tabname) {
		GridData gd = null;
		Color bgcolor = Display.getDefault().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
		
		BaseDataBind tabbind = new BaseDataBind();
		this.getDataBind().bind(tabid, tabbind);
		
		TabItem tabItme1 = new TabItem(tabFolder, SWT.NONE);
		tabItme1.setData("id", tabid);
		tabItme1.setText(tabname);

		Composite tabcp = new Composite(tabFolder, SWT.NONE);
		tabcp.setLayout(new GridLayout(1, false));
		gd = new GridData(GridData.FILL_BOTH);
		tabcp.setLayoutData(gd);
		tabcp.setBackground(bgcolor);
		
		Composite content = new Composite(tabcp, SWT.NONE);
		content.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		content.setLayout(new GridLayout(2, false));
		
		Label lb=  new Label(content,SWT.RIGHT);
		lb.setText("列数(colModel):");
		Text colModel = new Text(content, SWT.BORDER);
		colModel.setLayoutData(new GridData(200, 15));
		tabbind.bind("colModel", colModel);

		// 创建数据源区域的面板
		createDataSourceComposite(this,tabbind, tabcp, tabid);
		// 创建表单属性的表格信息面板
		createDateGridComposite(this,tabbind, tabcp, tabid);

		tabItme1.setControl(tabcp);
	}

	/**
	 * @param gridComposite
	 * @param cs
	 *            创建数据源区域的面板
	 */
	public void createDataSourceComposite(BaseDialog dialog, BaseDataBind tabbind,Composite tabcp,
			String tabid) {
		FormToolkit toolkit = new FormToolkit(tabcp.getDisplay());
		Color bgcolor = Display.getDefault().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
		ExpandableComposite ec = toolkit.createExpandableComposite(tabcp,
				ExpandableComposite.TREE_NODE
						| ExpandableComposite.NO_TITLE_FOCUS_BOX);
		ec.setBackground(bgcolor);
		ec.setData("dialog", dialog);
		ec.setText("根据SQL带出表單属性栏位");
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
//		gd.horizontalSpan=2;
		ec.setLayoutData(gd);
		ec.addExpansionListener(new ExpansionAdapter() {
			public void expansionStateChanged(ExpansionEvent e) {
				// 根据部件的新尺寸重新定位和更新滚动条
				Composite ec = (ExpandableComposite) e.getSource();
				ec.layout();

				BaseDialog dialog = (BaseDialog) ec.getData("dialog");
				dialog.getShell().pack(true);
				dialog.getShell().layout();
			}
		});

		Composite group1 = toolkit.createComposite(ec, SWT.NONE);
		group1.setLayout(new GridLayout(3, false));
		group1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		ec.setClient(group1);

		Label labe11 = new Label(group1, SWT.RIGHT);
		labe11.setText("DataSource:");
		Combo combo1 = new Combo(group1, SWT.READ_ONLY);
		String[] datasources = QMDao.getPanAllDataSource(false);
		combo1.setItems(datasources);
		combo1.setText(this.getQmDao().getDefaultDataSource());
		combo1.setLayoutData(new GridData(100, 24));
		
		for (int i = 0; i < datasources.length; i++) {
			combo1.setData(String.valueOf(i), datasources[i]);
		}
		
		this.getDataBind().bind("dataSource", combo1);

		Button button1 = new Button(group1, SWT.NONE);
		button1.setText("语句分析");

		Label labe12 = new Label(group1, SWT.RIGHT);
		labe12.setText("SQl:");		
		Text text2 = new Text(group1, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
		GridData text2GridData = new GridData(GridData.FILL_HORIZONTAL);
		text2GridData.horizontalSpan = 2;
		text2GridData.heightHint = 50;
		text2.setLayoutData(text2GridData);
		text2.setText(this.getQmDao().getDefaultSQL());
		this.getDataBind().bind("sql", text2);

		Label labe13 = new Label(group1, SWT.RIGHT);
		labe13.setText("可选栏位:");

		ViewForm vform = new ViewForm(group1, SWT.NONE);
		// vform.setLayout(new FillLayout());
		vform.setLayout(new GridLayout());
		GridData vformData = new GridData(GridData.FILL_HORIZONTAL);
		vformData.horizontalSpan = 2;
		vformData.heightHint = 150;
		vform.setLayoutData(vformData);

		PTableViewer tv1 = createTableViewer1(vform);
		CheckboxTableViewer ctv1 = new CheckboxTableViewer(tv1.getTable());
		BaseDataEntity data1 = this.getDataInstance();
		data1.spov(tabid + "tv1", tv1);
		data1.spov(tabid + "ctv1", ctv1);

		tv1.setContentProvider(new PllTableContentProvider()); // 内容器
		tv1.setLabelProvider(new PllTableLabelProvider(tv1));// 标签器

		SeachActionGroup actionGroup1 = new SeachActionGroup(this,tabid,"BasePanelDialog");
		actionGroup1.fillContextMenu(new MenuManager());
		ToolBar toolbar1 = new ToolBar(vform, SWT.FLAT | SWT.RIGHT);
		toolbar1.setBackground(bgcolor);
		ToolBarManager toolbarManager = new ToolBarManager(toolbar1);
		actionGroup1.fillActionToolbars(toolbarManager);

		vform.setContent(tv1.getControl());
		vform.setTopLeft(toolbar1);

		BaseDataEntity data2 = new BaseDataEntity();
		data2.spov("tv", tv1);
		data2.spov("bind", this.getDataBind());

		button1.addMouseListener(new PMouseListener(data2) {
			public void mouseUp(MouseEvent paramMouseEvent) {
				QMDao qmDao = QMDao.getInstance();
				BaseDataBind bind = (BaseDataBind) this.gpov("bind");
				PTableViewer tv1 = (PTableViewer) this.gpov("tv");
				tv1.setInput(qmDao.getColumnsInfo(bind.getValue("dataSource"),
						bind.getValue("sql")));
				// tv1.getPTable().ReLoadRecord(qmDao.getColumnsInfo(bind.getValue("dataSource"),bind.getValue("sql")));
			}
		});
	}

	/**
	 * @param group1
	 * @return 可选栏位的表格查看器
	 */
	public PTableViewer createTableViewer1(ViewForm vform) {
		PTableViewer tv1 = new PTableViewer(new PTable(vform, SWT.CHECK
				| SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER
				| SWT.FULL_SELECTION));
		PTable table = tv1.getPTable();
		table.setLayout(new GridLayout());
		GridData tableGridData = new GridData(GridData.FILL_BOTH);
		// tableGridData.horizontalSpan=2;
		// tableGridData.heightHint = 100;
		table.setLayoutData(tableGridData);
		table.setHeaderVisible(true); // 表头显示
		table.setLinesVisible(true); // 表格线显示

		TableLayout tLayout = new TableLayout();
		table.setLayout(tLayout);

		tLayout.addColumnData(new ColumnWeightData(20));
		PTableColumn col1 = new PTableColumn(table, SWT.NONE);
		col1.setText("名称");
		col1.setName("cid");

		tLayout.addColumnData(new ColumnWeightData(30));
		PTableColumn col2 = new PTableColumn(table, SWT.NONE);
		col2.setText("标题");
		col2.setName("caption");
		
		tv1.addDragSupport(DND.DROP_MOVE,new Transfer[] { ObjectTransfer.getInstance() },new ViewerDragAdapter(tv1));
		tv1.addDropSupport(DND.DROP_MOVE,new Transfer[] { ObjectTransfer.getInstance() }, new PanTableOrderDropAdapter(tv1));

		table.reloadColumns();
		return tv1;
	}

	/**
	 * @param gridComposite
	 * @param cs
	 *            创建表格信息面板
	 */
	public void createDateGridComposite(BaseDialog dialog,BaseDataBind tabbind, Composite tabcp,
			String tabid) {
		Group group1 = new Group(tabcp, SWT.NULL);
		group1.setText("已选栏位");
		group1.setLayout(new GridLayout(2, false));
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
//		gd.horizontalSpan=2;
		group1.setLayoutData(gd);

		ViewForm vform = new ViewForm(group1, SWT.NONE);
		// vform.setLayout(new FillLayout());
		vform.setLayout(new GridLayout());
		GridData vformData = new GridData(GridData.FILL_HORIZONTAL);
		vformData.horizontalSpan = 2;
		vformData.heightHint = 300;
		vform.setLayoutData(vformData);

		PTableViewer tv2 = createTableViewer2(vform);
		CheckboxTableViewer ctv2 = new CheckboxTableViewer(tv2.getTable());
		BaseDataEntity data1 = this.getDataInstance();
		data1.spov(tabid + "tv2", tv2);
		data1.spov(tabid + "ctv2", ctv2);

		tv2.setContentProvider(new PllTableContentProvider()); // 内容器
		tv2.setLabelProvider(new PllTableLabelProvider(tv2));// 标签器

		// XMLNode node = detailPage.getCurNode();
		// tv2.setInput(node.getChildBaseDataEntities());
		//
		SelectedColumnsActionGroup actionGroup1 = new SelectedColumnsActionGroup(this,tabid,"BasePanelDialog");
		actionGroup1.fillContextMenu(new MenuManager());
		ToolBar toolbar1 = new ToolBar(vform, SWT.FLAT | SWT.RIGHT);
		Color bgcolor = Display.getDefault().getSystemColor(
				SWT.COLOR_WIDGET_BACKGROUND);
		toolbar1.setBackground(bgcolor);
		ToolBarManager toolbarManager = new ToolBarManager(toolbar1);
		actionGroup1.fillActionToolbars(toolbarManager,false);

		vform.setContent(tv2.getControl());
		vform.setTopLeft(toolbar1);
	}

	/**
	 * @param group1
	 * @return 已选栏位的表格查看器
	 */
	public PTableViewer createTableViewer2(ViewForm vform) {
		PTableViewer tv2 = new PTableViewer(new PTable(vform, SWT.CHECK
				| SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER
				| SWT.FULL_SELECTION));
		PTable table = tv2.getPTable();
//		TableCursor cursor = new TableCursor(table, SWT.NONE);
//		cursor.addMouseListener(new PMouseListener() {
//			@Override
//			public void mouseDoubleClick(MouseEvent e) {
//				Utils.print(e.getSource().getClass().getName());
//			}
//		});
		
		table.setLayout(new GridLayout());
		GridData tableGridData = new GridData(GridData.FILL_BOTH);
		table.setLayoutData(tableGridData);
		table.setHeaderVisible(true);//表头显示
		table.setLinesVisible(true);//表格线显示

		PTableColumn col1 = new PTableColumn(table, SWT.NONE);
		col1.setText("名称");
		col1.setName("name");
		col1.setWidth(80);

		PTableColumn col2 = new PTableColumn(table, SWT.NONE);
		col2.setText("标题");
		col2.setName("caption");
		col2.setWidth(80);

		PTableColumn col3 = new PTableColumn(table, SWT.NONE);
		col3.setText("宽度");
		col3.setName("width");
		col3.setWidth(40);
		
		PTableColumn col4 = new PTableColumn(table, SWT.NONE);
		col4.setText("类型");
		col4.setName("xtype_text");
		col4.setWidth(60);
		
		PTableColumn col5 = new PTableColumn(table, SWT.NONE);
		col5.setText("為空");
		col5.setName("required_text");
		col5.setWidth(70);
		
		PTableColumn col6 = new PTableColumn(table, SWT.NONE);
		col6.setText("验证");
		col6.setName("valid");
		col6.setWidth(60);
		
		PTableColumn col7 = new PTableColumn(table, SWT.NONE);
		col7.setText("明細");
		col7.setName("detail");
		col7.setWidth(80);
		
		table.reloadColumns();
		tv2.setColumnProperties(new String[] { "name", "caption", "width","xtype_text","required_text","valid","detail" });
		CellEditor[] editors = new CellEditor[5];
		editors[0] = new TextCellEditor(table, SWT.NONE);
		editors[1] = new TextCellEditor(table, SWT.NONE);
		editors[2] = new TextCellEditor(table, SWT.NONE);
		
		Map<String ,String> editorMap = QMDao.getInstance().getEditorMap();
		PanStringComboBoxCellEditor combo1 = new PanStringComboBoxCellEditor(table,editorMap.keySet().toArray(new String[]{}), SWT.READ_ONLY);
		Iterator it = editorMap.keySet().iterator();
		while(it.hasNext()){
			String key = (String)it.next();
			combo1.setData(key, editorMap.get(key));
		}
		editors[3]=combo1;
		
		PanStringComboBoxCellEditor combo2 = new PanStringComboBoxCellEditor(table,new String[] { "Y","N" }, SWT.READ_ONLY);
		combo2.setData("Y", "false");
		combo2.setData("N", "true");
		editors[4]=combo2;
		
		List list = new ArrayList();
		list.add("name");
		list.add("caption");
		list.add("width");
		list.add("xtype_text");
		list.add("required_text");
		tv2.setCellEditors(editors);
		tv2.setCellModifier(new SelectColumnsTableCellModifier(tv2,list));

		tv2.addDragSupport(DND.DROP_MOVE,new Transfer[] { ObjectTransfer.getInstance() },new ViewerDragAdapter(tv2));
		tv2.addDropSupport(DND.DROP_MOVE,new Transfer[] { ObjectTransfer.getInstance() }, new PanTableOrderDropAdapter(tv2));
		
		BaseDataEntity data = new BaseDataEntity();
		data.spov("tv2", tv2);
		tv2.getTable().addMouseListener(new PMouseListener(data){
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				PTableViewer tv2 = (PTableViewer)this.getData().gpov("tv2");
				PTable table = tv2.getPTable();
				int rowIndex = table.getSelectionIndex();
				int columnIndex = -1;
				int left = 0;
				for (int i = 0; i < table.getColumns().length; i++) {
					left += table.getColumns()[i].getWidth();
					if (e.x < left) {
						columnIndex = i;
						break;
					}
				}
				if (columnIndex == 6) {
					TableItem tabitem = table.getItem(rowIndex);
					BaseDataEntity data = (BaseDataEntity)tabitem.getData();
					if(data==null)return;
					
					BaseDialog dialog = getDialog(data);
					if (dialog != null) {
						//初始化控件 的值
						BaseDataEntity defaultData = dialog.getDefaultData();
						if(defaultData == null){
							defaultData = new BaseDataEntity();
							dialog.setDefaultData(defaultData);
						}
						
						data.CopyTo(defaultData, false);
						if(dialog.open() == BaseDialog.OK){
							table.ReloadTableItemRecord(tabitem, data);
						}
		            }
				}
			}
		});
		return tv2;
	}
	
	public BaseDialog getDialog(BaseDataEntity row){
		String xtype = row.gpsv("xtype");
		String strClass = this.classDialogMap.get(xtype);
		BaseDialog dialog = null;
		if(!StringHelper.IsNullOrEmpty(strClass)){
			dialog = (BaseDialog)ObjectHelper.newInstance(strClass, new Object[]{Display.getDefault().getActiveShell(),row,"fillConfig"});
		}
		return dialog;
	}
	
	public FormItem getFormItem(String xtype){
		if(StringHelper.IsNullOrEmpty(xtype)) return null;
		String strClass = this.classFormItemMap.get(xtype);
		FormItem formItem = null;
		if(!StringHelper.IsNullOrEmpty(strClass)){
			formItem = (FormItem)ObjectHelper.newInstance(strClass,null);
		}
		return formItem;
	}

	@Override
	public void initRootItem(BaseDataEntity data, BaseDataBind bind,
			StringBuilderEx code) {
		if (bind == null)
			return;
		BaseDataEntity record = bind.getRecord();
		//1.表单
		FormItem panel = getNewFormItem();
		if (panel == null)
			return;
		panel.spsv("id", record.gpsv("id"));
		panel.spsv("data", "<%=Utils.TAG_FORM%>");
		panel.spsv("tabSize", record.gpsv("tabSize"));
		panel.spsnnv("customtab", record.gpsv("customtab"));
		
		TabFolder tabFolder = getTabFolder();
		QMDao dao = QMDao.getInstance();
		for (int i = 0; i < tabFolder.getItems().length; i++) {
			//2.tab页
			TabItem tabItem = tabFolder.getItem(i);
			String tabid = (String)tabItem.getData("id");
			BaseDataEntity tabRecord = (BaseDataEntity)record.gpov(tabid);
			CheckboxTableViewer ctv2 = (CheckboxTableViewer)this.getDataInstance().gpov(tabid+"ctv2");
			Tab tab = new Tab();
			tab.spsv("colModel", tabRecord.gpsv("colModel","*"));
			//3.form控件
			for (int j = 0; j < ctv2.getTable().getItems().length; j++) {
				TableItem tabitem = ctv2.getTable().getItems()[j];
				BaseDataEntity row = (BaseDataEntity)tabitem.getData();
				dao.registerPanICaption(row.gpsv("name"), row.gpsv("caption"));
				if(row != null){
					FormItem item = getFormItem(row.gpsv("xtype"));
					if(item != null){
						item.configItem(row,"default");
						tab.addItem(item);
					}else{
						Utils.print(row.ToJSONString());
					}
				}
			}
			panel.addItem(tab);
		}
		this.setRoot(panel);

	}
	
	public String getFirstTabName() {
		return "基本信息";
	}
	
	public String getSecTabName() {
		return "其它信息";
	}

	public int getCurTabSize() {
		return curTabSize;
	}

	public void setCurTabSize(int curTabSize) {
		this.curTabSize = curTabSize;
	}
	
	public TabFolder getTabFolder() {
		return tabFolder;
	}
}
